package com.zth.Service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zth.Service.UserService;
import com.zth.dao.UserDao;
import com.zth.entity.MyPage;
import com.zth.entity.User;
import com.zth.util.MD5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service//业务处理层

public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findAll() {
        return userDao.queryUser();
    }

    //登录 1.1
    @Override
    public User loginDev(User user) {

        User userinfoByName = userDao.queryUserinfoByName(user.getUsername());
        if (userinfoByName!=null){
            String loginPass = user.getPassword();
            String salt = userinfoByName.getSalt();
            String newPass = DigestUtils.md5DigestAsHex((loginPass + salt).getBytes());
            if (newPass.equals(userinfoByName.getPassword())){
                return userinfoByName;
            }
        }
        return null;
    }

    //注册 1.2
    @Override
    public Boolean reg(User userinfo) {
        String salt = UUID.randomUUID().toString();
        String oldPassword = userinfo.getPassword() + salt;
        String newPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        userinfo.setPassword(newPassword);
        userinfo.setSalt(salt);
        return userDao.reg(userinfo);
    }


    //分页
    @Override
    public PageInfo queryByPage(MyPage page) {
        PageHelper.startPage(page.getPage(),page.getLimit());
        List<User> users = userDao.queryUser();
        return new PageInfo<>(users);
    }


    @Override
    public Boolean updStatus(User user) {
        Integer integer = userDao.updStatus(user);
        return integer>0;
    }

    @Override
    public Boolean updUserinfo(User user) {
        Integer integer = userDao.updUserinfo(user);
        return integer>0;
    }

    @Override
    public User queryById(Integer id) {
        return userDao.queryById(id);
    }


}
